package com.furn.service.impl;

import com.furn.bean.Furn;
import com.furn.bean.FurnExample;
import com.furn.dao.FurnMapper;
import com.furn.service.FurnService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class FurnServiceImpl implements FurnService {
    @Resource
    private FurnMapper furnMapper;

    @Override
    public void save(Furn furn) {
        //使用insertSelective方法
        //因为id是属于自增长的，所以要使用insertSelective
        furnMapper.insertSelective(furn);
    }

    @Override
    public List<Furn> findAll() {
        //查看分析FurnMapper.xml文件
        //可以分析出将参数设置为null，就意味着查询所有的家具信息
        return furnMapper.selectByExample(null);
    }

    @Override
    public void update(Furn furn) {
        furnMapper.updateByPrimaryKeySelective(furn);

    }

    @Override
    public void delete(Integer id) {
        furnMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Furn findFurnById(Integer id) {
        return furnMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Furn> findByCondition(String name) {
        FurnExample furnExample = new FurnExample();
        //可以通过Criteria设置查询条件
        furnExample.setOrderByClause("id DESC");
        FurnExample.Criteria criteria = furnExample.createCriteria();
        if (StringUtils.hasText(name)) {//如果name有值
            criteria.andNameLike("%" + name + "%");
        }
        //如果name没有传值，那么返回的依然是查询所有记录
        return furnMapper.selectByExample(furnExample);
    }
}
